package seatechit.msbgateway.consumer;

import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import seatechit.msbgateway.dbaccess.entity.HOSTMessagLogInfo;
import seatechit.msbgateway.dbaccess.entity.SalaryBKDDSALW;
import seatechit.msbgateway.dbaccess.entity.SalaryBKDDSALWH;
import seatechit.msbgateway.proxy.as400gw.Messages;
import seatechit.msbgateway.utils.AppUtils;
import seatechit.msbgateway.utils.CachedParam;
import seatechit.msbgateway.utils.CoreBankUtils;
import seatechit.msbgateway.utils.ElementUtils;
import seatechit.msbgateway.utils.Global;

import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbMessage;

public class SalaryConsumer extends SIBSConsumer {
	final Logger logger = Logger.getLogger(SalaryConsumer.class);
	final String CLASS_NAME = this.getClass().getName();

	public SalaryConsumer() {
		super();
	}

	@Override
	public void submit(MbMessage inMessage, MbMessage outMessage) {
		try {
			this.inMessage = inMessage;
			this.outMessage = outMessage;

			this.baseXmlRequestDoc = ElementUtils.getXMLContent(inMessage);
			this.buildLogOfRequestMessage();

			String tran_code = xmlMessageLogInfo.getTran_code();
			String tran_service_code = xmlMessageLogInfo.getTran_service_code();
			buildTransactionInfo(consumerType, tran_code, tran_service_code);
			this.hostMessageLogInfo.setTran_sn(xmlMessageLogInfo.getSender_tran_sn());

			saveSalary();

			// 3)add more detail response time,date....
			this.buildLogOfResponseMessage();
			this.xmlMessageLogInfo.setExecution_date(hostMessageLogInfo.getHost_tran_date());
			this.xmlMessageLogInfo.setRef_tran_no(hostMessageLogInfo.getHost_tran_sn());
			this.buildXMLMessageResponseHeader();
			this.baseXmlResponseDoc = ElementUtils.getXMLContent(outMessage);
			this.buildLogMessageInfo();
			this.logMessageFacade.insertLogMessage(logMessage);
			System.out.println(getLogTradeMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(new StringBuffer().append("Error source:").append(CLASS_NAME + ".submit()\n").append("Error message:").append(ex.toString() + "\n"));
			this.retErrorInfo.setErrorCode(Global.ERR_SYSTEM_UNKNOWN_ERROR);
			this.retErrorInfo.setErrorMessage(ex.toString());
		}
	}

	/**
	 * 
	 */
	private void saveSalary() {
		try {

			String UploadTime = AppUtils.now("hhmmss");
			String hostTranDate = this.hostMessageLogInfo.getHost_tran_date().length() < 6 ? "0" + hostMessageLogInfo.getHost_tran_date() : hostMessageLogInfo.getHost_tran_date();
			String UploadDate = hostTranDate;
			String NextTransferDate = AppUtils.getJulian7FromDate((Date) (new SimpleDateFormat(Global.DEF_FORMAT_DATE_DDMMYY)).parse(hostTranDate));

			// List
			List<MbElement> listOfTransfer = (List<MbElement>) this.inMessage.evaluateXPath("/message/body/list[@name='records']/entity");
			ArrayList<SalaryBKDDSALW> arrSalaryBKDDSALW = new ArrayList<SalaryBKDDSALW>();
			ArrayList<SalaryBKDDSALWH> arrSalaryBKDDSALWH = new ArrayList<SalaryBKDDSALWH>();
			SalaryBKDDSALWH tempSalaryBKDDSALWH = new SalaryBKDDSALWH();
			double totalAmt = 0;
			String rollout_branch_no = ElementUtils.getXMLElementInStringValue(inMessage, "/message/body/list[@name='records']/entity[@name='0']/field[@name='rollout_branch_no']");
			String seqNumber = sysParamFacade.getTransactionSequence();
			String strNew_RefNumber = rollout_branch_no + "9" + AppUtils.padLeft(seqNumber, 6, '0');
			String rollout_acct_no = "";
			String rollout_acct_ccy = "";
			String gl_acct_no = CachedParam.getSystemParam(Global.PARAM_GL_SALARY_PAYMENT);// "280701007";
			for (int i = 0; i < listOfTransfer.size(); i++) {
				ErrorInfo tempErrorInfo = new ErrorInfo(ERR_SYSTEM_OK, ERR_SYSTEM_OK_MSG, MESSAGE_TYPE_ABCS);
				SalaryBKDDSALW tempSalaryBKDDSALW = new SalaryBKDDSALW();

				String xmlInputPattern_2 = "/message/body/list[@name='records']/entity[@name='" + (i) + "']/field[@name='%s']";
				String tran_sn = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "tran_sn"));
				rollout_branch_no = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "rollout_branch_no"));
				String bnfc_branch_no = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "bnfc_branch_no"));
				rollout_acct_no = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "rollout_acct_no"));
				String rollout_acct_type = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "rollout_acct_type"));
				rollout_acct_ccy = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "rollout_acct_ccy"));
				String rollout_buy_rate = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "rollout_buy_rate"));

				String bnfc_acct_no = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "bnfc_acct_no"));
				String bnfc_acct_type = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "bnfc_acct_type"));
				String bnfc_acct_ccy = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "bnfc_acct_ccy"));
				String bnfc_sell_rate = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "bnfc_sell_rate"));
				String amt = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "amt"));
				String remark = ElementUtils.getXMLElementInStringValue(inMessage, String.format(xmlInputPattern_2, "remark"));

				if (tran_sn.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_TRAN_SN_NOT_VALID_OR_MISSING);
				}

				if (rollout_branch_no.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_SRC_BRANCH_CODE_NOT_VALID_OR_MISSING);
				}
				if (rollout_acct_no.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_SRC_ACCOUNT_NO_NOT_VALID_OR_MISSING);
				}
				if (rollout_acct_ccy.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_SRC_CURRENCY_CODE_NOT_VALID_OR_MISSING);
				}
				if (rollout_buy_rate.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_SELL_RATE_NOT_VALID_OR_MISSING);
				}
				if (rollout_acct_type.length() == 0 || CoreBankUtils.isValidAccountType(rollout_acct_type) == false) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_SRC_ACCOUNT_TYPE_NOT_VALID_OR_MISSING);
				}

				if (!AppUtils.isValidNumber(amt)) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_TRANSACTION_AMOUNT_NOT_VALID_OR_MISSING);
				}

				if (bnfc_branch_no.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_DES_BRANCH_CODE_NOT_VALID_OR_MISSING);
				}
				if (bnfc_acct_no.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_DES_ACCOUNT_NO_NOT_VALID_OR_MISSING);
				}
				if (bnfc_acct_type.length() == 0 || CoreBankUtils.isValidAccountType(bnfc_acct_type) == false) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_DES_ACCOUNT_TYPE_NOT_VALID_OR_MISSING);
				}
				if (bnfc_acct_ccy.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_DES_CURRENCY_CODE_NOT_VALID_OR_MISSING);
				}
				if (bnfc_sell_rate.length() == 0) {
					tempErrorInfo.setErrorCode(Global.ERR_UDF_BUY_RATE_NOT_VALID_OR_MISSING);
				}
				totalAmt += Double.parseDouble(amt);
				tempSalaryBKDDSALW.setSDID(strNew_RefNumber);
				tempSalaryBKDDSALW.setSDAMT(amt);
				tempSalaryBKDDSALW.setSDBRN(rollout_branch_no);
				tempSalaryBKDDSALW.setSDRCID("I");
				tempSalaryBKDDSALW.setSDCTYP("VND");
				tempSalaryBKDDSALW.setSDSEQ(String.valueOf(i + 1));
				tempSalaryBKDDSALW.setSDACCT(bnfc_acct_no);
				tempSalaryBKDDSALW.setSDATYP(bnfc_acct_type);
				tempSalaryBKDDSALW.setSDCOST("0");
				tempSalaryBKDDSALW.setSDPROC("0");
				tempSalaryBKDDSALW.setSDSTAT("0");
				tempSalaryBKDDSALW.setSDAMT2("0");
				tempSalaryBKDDSALW.setSDAVL("0");
				tempSalaryBKDDSALW.setSDMUID("CNTT");
				tempSalaryBKDDSALW.setSDMWID("CNTT");
				tempSalaryBKDDSALW.setSDMTIM(UploadTime);
				tempSalaryBKDDSALW.setSDDAT6(UploadDate);
				tempSalaryBKDDSALW.setSDDAT7(NextTransferDate);
				tempSalaryBKDDSALW.setSDACUR("0");
				tempSalaryBKDDSALW.setSSRSV1("");
				tempSalaryBKDDSALW.setSSRSV2("");
				tempSalaryBKDDSALW.setREMRK1(remark);
				tempSalaryBKDDSALW.setREMRK2("");
				arrSalaryBKDDSALW.add(tempSalaryBKDDSALW);
			}

			tempSalaryBKDDSALWH.setSMACCT(gl_acct_no); // gl trung gian
			tempSalaryBKDDSALWH.setSMID(strNew_RefNumber);
			tempSalaryBKDDSALWH.setSMRCID("I");
			tempSalaryBKDDSALWH.setSMCOST("0");
			tempSalaryBKDDSALWH.setSMPROC("0");
			tempSalaryBKDDSALWH.setSMATYP("G");
			tempSalaryBKDDSALWH.setSMBRN(rollout_branch_no);
			tempSalaryBKDDSALWH.setSMCTYP("VND");
			tempSalaryBKDDSALWH.setSMSTAT("0");
			tempSalaryBKDDSALWH.setSMAMT(String.valueOf(totalAmt));
			tempSalaryBKDDSALWH.setSMAVL("0");
			tempSalaryBKDDSALWH.setSMCNT("0");
			tempSalaryBKDDSALWH.setSMAMT2("0");
			tempSalaryBKDDSALWH.setSMCNT2("0");
			tempSalaryBKDDSALWH.setSSMUID("CNTT");
			tempSalaryBKDDSALWH.setSSMWID("CNTT");
			tempSalaryBKDDSALWH.setSSMTIM(UploadTime);
			tempSalaryBKDDSALWH.setSMDAT6(UploadDate);
			tempSalaryBKDDSALWH.setSMDAT7(NextTransferDate);
			tempSalaryBKDDSALWH.setSMTFR6(UploadDate);
			tempSalaryBKDDSALWH.setSMTFR7(NextTransferDate);
			tempSalaryBKDDSALWH.setSMLST6("0");
			tempSalaryBKDDSALWH.setSMLST7("0");
			tempSalaryBKDDSALWH.setSSRSV1("");
			tempSalaryBKDDSALWH.setSSRSV2("");
			tempSalaryBKDDSALWH.setSMRMK1("");
			tempSalaryBKDDSALWH.setSMRMK2("");
			arrSalaryBKDDSALWH.add(tempSalaryBKDDSALWH);

			// 1)ghi no ca->gl
			String core_sn = hostMessageLogInfo.getHost_tran_sn();
			ErrorInfo tempErrorInfo = transferCAGL(core_sn, hostMessageLogInfo.getHost_tran_date(), rollout_branch_no, rollout_acct_no, rollout_acct_ccy, gl_acct_no, String
					.valueOf(totalAmt));
			if (tempErrorInfo.getErrorCode() == ERR_SYSTEM_OK) {
				// 2)insert vao bang tam cua core
				if (salaryFacade.saveSalary(arrSalaryBKDDSALW, arrSalaryBKDDSALWH)) {
					// 3.1 Add ref
					core_sn = sysParamFacade.getTransactionSequence();
					tempErrorInfo = addRefNumber(core_sn, strNew_RefNumber, rollout_branch_no, rollout_acct_ccy);
					if (tempErrorInfo.getErrorCode() == ERR_SYSTEM_OK) {
						// Lock row
						core_sn = sysParamFacade.getTransactionSequence();
						tempErrorInfo = salaryLockApprove(core_sn, strNew_RefNumber, rollout_branch_no, rollout_acct_ccy);
						if (tempErrorInfo.getErrorCode() == ERR_SYSTEM_OK) {
							// 3.2 Duyet
							core_sn = sysParamFacade.getTransactionSequence();
							tempErrorInfo = salaryApprove(core_sn, strNew_RefNumber, rollout_branch_no, rollout_acct_ccy);
							if (tempErrorInfo.getErrorCode() == ERR_SYSTEM_OK) {
								// 3.3 Posting
								// core_sn =
								// tranParamFacade.getTransactionSequence();
								// tempErrorInfo = onlinePosting(core_sn,
								// strNew_RefNumber, rollout_branch_no,
								// rollout_acct_ccy);
							}
						}
					}
				}
			}
			this.retErrorInfo = tempErrorInfo;
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(new StringBuffer().append("Error source:").append(CLASS_NAME + ".saveSalary()\n").append("Error message:").append(ex.toString() + "\n"));
			this.retErrorInfo.setErrorCode(ERR_SYSTEM_UNKNOWN_ERROR);
		}
	}

	/**
	 * 
	 * @param core_sn
	 * @param effective_date
	 * @param rollout_branch_no
	 * @param rollout_acct_no
	 * @param rollout_acct_ccy
	 * @param gl_acct_no
	 * @param totalAmt
	 * @return
	 */
	private ErrorInfo transferCAGL(String core_sn, String effective_date, String rollout_branch_no, String rollout_acct_no, String rollout_acct_ccy, String gl_acct_no,
			String totalAmt) {
		ErrorInfo tempErrorInfo = new ErrorInfo();
		try {
			String hostTranCode = "1471";
			ABCS_TEMPLATE[2] = hostMessageLogInfo.getWs_ip();
			ABCS_TEMPLATE[18] = "BBHTLMONEYFNC";
			ABCS_TEMPLATE[25] = hostMessageLogInfo.getTeller_id();
			ABCS_TEMPLATE[26] = hostMessageLogInfo.getWs_name();
			ABCS_TEMPLATE[27] = hostTranCode;
			ABCS_TEMPLATE[34] = hostMessageLogInfo.getWs_ip();
			ABCS_TEMPLATE[42] = hostMessageLogInfo.getTeller_id();
			ABCS_TEMPLATE[43] = core_sn;
			ABCS_TEMPLATE[48] = hostTranCode;
			ABCS_TEMPLATE[49] = hostMessageLogInfo.getHost_tran_date();
			ABCS_TEMPLATE[53] = hostMessageLogInfo.getApprover_id();
			ABCS_TEMPLATE[54] = rollout_branch_no;
			ABCS_TEMPLATE[64] = rollout_acct_no;
			ABCS_TEMPLATE[67] = totalAmt;
			ABCS_TEMPLATE[68] = effective_date;
			ABCS_TEMPLATE[72] = gl_acct_no;
			ABCS_TEMPLATE[83] = String.valueOf(totalAmt);
			ABCS_TEMPLATE[88] = "10000000" + "00";
			ABCS_TEMPLATE[89] = "10000000" + "00";
			ABCS_TEMPLATE[94] = "VND"; // base_ccy
			ABCS_TEMPLATE[106] = rollout_acct_ccy;
			ABCS_TEMPLATE[107] = rollout_acct_ccy;
			ABCS_TEMPLATE[250] = String.valueOf(totalAmt);
			String hostSendMessage = AppUtils.convertArrayToString(ABCS_TEMPLATE);
			String hostReceiveMessage = "";
			Messages retMessage;
			hostMessageLogInfo.setSend_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			retMessage = as400Port.hostMessageSending(hostMessageLogInfo.getRef_channel(), hostSendMessage);
			hostMessageLogInfo.setReceive_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			hostMessageLogInfo.setNumOfRetry(String.valueOf(1));
			if (retMessage != null) {
				hostReceiveMessage = AppUtils.convertArrayToString(retMessage.getArrString());
				if (retMessage.getErrCode() == null) {
				} else {
					tempErrorInfo.setErrorCode(Global.ERR_MBASE_START);
					tempErrorInfo.setErrorMessage(retMessage.getDescription());
				}
			}
			String refTranCode = "SAL_081";
			this.hostMessageLogInfo.setRef_tran_code(refTranCode);
			this.hostMessageLogInfo.setHost_tran_sn(core_sn);
			this.hostMessageLogInfo.setResp_code(String.valueOf(tempErrorInfo.getErrorCode()));
			this.hostMessageLogInfo.setResp_msg(tempErrorInfo.getErrorMessage());
			this.hostMessageLogInfo.setHostRequestMsg(hostSendMessage);
			this.hostMessageLogInfo.setHostResponseMsg(hostReceiveMessage);
			this.arrRetHOSTMessageLogInfo.add((HOSTMessagLogInfo) hostMessageLogInfo.clone());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return tempErrorInfo;
	}

	/**
	 * 
	 * @param core_sn
	 * @param refNumber
	 * @param branch_code
	 * @param currency
	 * @return
	 */
	private ErrorInfo addRefNumber(String core_sn, String refNumber, String branch_code, String currency) {
		ErrorInfo tempErrorInfo = new ErrorInfo();
		try {
			String hostTranCode = "28902";
			MBASE_TEMPLATE[2] = hostMessageLogInfo.getWs_ip();
			MBASE_TEMPLATE[18] = "BBMBSDDMNTFNC";
			MBASE_TEMPLATE[23] = "1";
			MBASE_TEMPLATE[25] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[26] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[27] = hostTranCode;
			MBASE_TEMPLATE[32] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[40] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[42] = branch_code;// hostMessageLogInfo.getBranch_code();
			MBASE_TEMPLATE[46] = hostTranCode;
			MBASE_TEMPLATE[47] = "C";
			MBASE_TEMPLATE[49] = "1";
			MBASE_TEMPLATE[68] = branch_code;
			MBASE_TEMPLATE[69] = refNumber;
			MBASE_TEMPLATE[70] = currency;

			String hostSendMessage = AppUtils.convertArrayToString(MBASE_TEMPLATE);
			String hostReceiveMessage = "";
			Messages retMessage;

			hostMessageLogInfo.setSend_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			retMessage = as400Port.hostMessageSending(hostMessageLogInfo.getRef_channel(), hostSendMessage);
			hostMessageLogInfo.setReceive_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			hostMessageLogInfo.setNumOfRetry(String.valueOf(1));

			if (retMessage != null) {
				hostReceiveMessage = AppUtils.convertArrayToString(retMessage.getArrString());
				if (retMessage.getErrCode() == null) {
				} else {
					tempErrorInfo.setErrorCode(Global.ERR_MBASE_START);
					tempErrorInfo.setErrorMessage(retMessage.getDescription());
				}
			}
			String refTranCode = "SAL_082";
			this.hostMessageLogInfo.setRef_tran_code(refTranCode);
			this.hostMessageLogInfo.setHost_tran_sn(core_sn);
			this.hostMessageLogInfo.setResp_code(String.valueOf(tempErrorInfo.getErrorCode()));
			this.hostMessageLogInfo.setResp_msg(tempErrorInfo.getErrorMessage());
			this.hostMessageLogInfo.setHostRequestMsg(hostSendMessage);
			this.hostMessageLogInfo.setHostResponseMsg(hostReceiveMessage);
			this.arrRetHOSTMessageLogInfo.add((HOSTMessagLogInfo) hostMessageLogInfo.clone());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return tempErrorInfo;
	}

	/**
	 * 
	 * @param core_sn
	 * @param refNumber
	 * @param branch_code
	 * @param currency
	 * @return
	 */
	private ErrorInfo salaryLockApprove(String core_sn, String refNumber, String branch_code, String currency) {
		ErrorInfo tempErrorInfo = new ErrorInfo();
		try {

			String hostTranCode = "26205";
			MBASE_TEMPLATE[2] = hostMessageLogInfo.getWs_ip();
			MBASE_TEMPLATE[18] = "BBMBSDDINQFNC";
			MBASE_TEMPLATE[25] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[26] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[27] = hostTranCode;
			MBASE_TEMPLATE[32] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[40] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[42] = branch_code;// hostMessageLogInfo.getBranch_code();
			MBASE_TEMPLATE[46] = hostTranCode;

			MBASE_TEMPLATE[47] = "I";
			MBASE_TEMPLATE[48] = "R";
			MBASE_TEMPLATE[49] = "1";
			MBASE_TEMPLATE[50] = "N";
			MBASE_TEMPLATE[51] = "F";
			MBASE_TEMPLATE[57] = "C";

			MBASE_TEMPLATE[68] = branch_code;
			MBASE_TEMPLATE[69] = refNumber;
			MBASE_TEMPLATE[70] = currency;

			String hostSendMessage = AppUtils.convertArrayToString(MBASE_TEMPLATE);
			String hostReceiveMessage = "";
			Messages retMessage;

			hostMessageLogInfo.setSend_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			retMessage = as400Port.hostMessageSending(hostMessageLogInfo.getRef_channel(), hostSendMessage);
			hostMessageLogInfo.setReceive_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			hostMessageLogInfo.setNumOfRetry(String.valueOf(1));

			if (retMessage != null) {
				hostReceiveMessage = AppUtils.convertArrayToString(retMessage.getArrString());
				if (retMessage.getErrCode() == null) {
				} else {
					tempErrorInfo.setErrorCode(Global.ERR_MBASE_START);
					tempErrorInfo.setErrorMessage(retMessage.getDescription());
				}
			}
			String refTranCode = "SAL_082";
			this.hostMessageLogInfo.setRef_tran_code(refTranCode);
			this.hostMessageLogInfo.setHost_tran_sn(core_sn);
			this.hostMessageLogInfo.setResp_code(String.valueOf(tempErrorInfo.getErrorCode()));
			this.hostMessageLogInfo.setResp_msg(tempErrorInfo.getErrorMessage());
			this.hostMessageLogInfo.setHostRequestMsg(hostSendMessage);
			this.hostMessageLogInfo.setHostResponseMsg(hostReceiveMessage);
			this.arrRetHOSTMessageLogInfo.add((HOSTMessagLogInfo) hostMessageLogInfo.clone());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return tempErrorInfo;
	}

	/**
	 * 
	 * @param core_sn
	 * @param refNumber
	 * @param branch_code
	 * @param currency
	 * @return
	 */
	private ErrorInfo salaryApprove(String core_sn, String refNumber, String branch_code, String currency) {
		ErrorInfo tempErrorInfo = new ErrorInfo();
		try {

			String hostTranCode = "28903";
			MBASE_TEMPLATE[2] = hostMessageLogInfo.getWs_ip();
			MBASE_TEMPLATE[18] = "BBMBSDDMNTFNC";
			MBASE_TEMPLATE[25] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[26] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[27] = hostTranCode;
			MBASE_TEMPLATE[32] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[40] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[42] = branch_code;// hostMessageLogInfo.getBranch_code();
			MBASE_TEMPLATE[46] = hostTranCode;
			MBASE_TEMPLATE[67] = "";
			MBASE_TEMPLATE[68] = branch_code;
			MBASE_TEMPLATE[69] = refNumber;
			MBASE_TEMPLATE[70] = currency;

			String hostSendMessage = AppUtils.convertArrayToString(MBASE_TEMPLATE);
			String hostReceiveMessage = "";
			Messages retMessage;

			hostMessageLogInfo.setSend_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			retMessage = as400Port.hostMessageSending(hostMessageLogInfo.getRef_channel(), hostSendMessage);
			hostMessageLogInfo.setReceive_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			hostMessageLogInfo.setNumOfRetry(String.valueOf(1));

			if (retMessage != null) {
				hostReceiveMessage = AppUtils.convertArrayToString(retMessage.getArrString());
				if (retMessage.getErrCode() == null) {
				} else {
					tempErrorInfo.setErrorCode(Global.ERR_MBASE_START);
					tempErrorInfo.setErrorMessage(retMessage.getDescription());
				}
			}
			String refTranCode = "SAL_083";
			this.hostMessageLogInfo.setRef_tran_code(refTranCode);
			this.hostMessageLogInfo.setHost_tran_sn(core_sn);
			this.hostMessageLogInfo.setResp_code(String.valueOf(tempErrorInfo.getErrorCode()));
			this.hostMessageLogInfo.setResp_msg(tempErrorInfo.getErrorMessage());
			this.hostMessageLogInfo.setHostRequestMsg(hostSendMessage);
			this.hostMessageLogInfo.setHostResponseMsg(hostReceiveMessage);
			this.arrRetHOSTMessageLogInfo.add((HOSTMessagLogInfo) hostMessageLogInfo.clone());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return tempErrorInfo;
	}

	/**
	 * 
	 * @param core_sn
	 * @param refNumber
	 * @param branch_code
	 * @param currency
	 * @return
	 */
	private ErrorInfo onlinePosting(String core_sn, String refNumber, String branch_code, String currency) {
		ErrorInfo tempErrorInfo = new ErrorInfo();
		try {
			String hostTranCode = "28904";
			MBASE_TEMPLATE[2] = hostMessageLogInfo.getWs_ip();
			MBASE_TEMPLATE[18] = "BBMBSDDMNTFNC";
			MBASE_TEMPLATE[25] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[26] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[27] = hostTranCode;
			MBASE_TEMPLATE[32] = hostMessageLogInfo.getTeller_id();
			MBASE_TEMPLATE[40] = hostMessageLogInfo.getWs_name();
			MBASE_TEMPLATE[42] = branch_code;// hostMessageLogInfo.getBranch_code();
			MBASE_TEMPLATE[46] = hostTranCode;
			MBASE_TEMPLATE[47] = "C";
			MBASE_TEMPLATE[69] = "";
			MBASE_TEMPLATE[70] = refNumber;
			MBASE_TEMPLATE[71] = currency;

			String hostSendMessage = AppUtils.convertArrayToString(MBASE_TEMPLATE);
			String hostReceiveMessage = "";
			Messages retMessage;

			hostMessageLogInfo.setSend_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			retMessage = as400Port.hostMessageSending(hostMessageLogInfo.getRef_channel(), hostSendMessage);
			hostMessageLogInfo.setReceive_time(AppUtils.now(Global.DEF_FORMAT_DATE_TIME_FULL));
			hostMessageLogInfo.setNumOfRetry(String.valueOf(1));

			if (retMessage != null) {
				hostReceiveMessage = AppUtils.convertArrayToString(retMessage.getArrString());
				if (retMessage.getErrCode() == null) {
				} else {
					tempErrorInfo.setErrorCode(Global.ERR_MBASE_START);
					tempErrorInfo.setErrorMessage(retMessage.getDescription());
				}
			}
			this.hostMessageLogInfo.setRef_tran_code("SAL_084");
			this.hostMessageLogInfo.setHost_tran_sn(core_sn);
			this.hostMessageLogInfo.setResp_code(String.valueOf(tempErrorInfo.getErrorCode()));
			this.hostMessageLogInfo.setResp_msg(tempErrorInfo.getErrorMessage());
			this.hostMessageLogInfo.setHostRequestMsg(hostSendMessage);
			this.hostMessageLogInfo.setHostResponseMsg(hostReceiveMessage);
			this.arrRetHOSTMessageLogInfo.add((HOSTMessagLogInfo) hostMessageLogInfo.clone());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return tempErrorInfo;
	}

}
